/**
 * Created by Administrator on 2016/9/20.
 */
$(function(){
    var li1=$("#header .ul2 .li1");
    var li1a=$("#header .ul2 .li1 a");
    var oDiv=$("#header .ul2 .li1 .own");
    var oDiva=$("#header .ul2 .li1 .own a");
    var li2=$("#header .ul2 .li2");
    var li2a=$("#header .ul2 .li2 a");
    var oDiv2=$("#header .ul2 .li2 .own");
    var oDiva2=$("#header .ul2 .li2 .own a");
    var li3=$("#header .ul2 .li3");
    var li3a=$("#header .ul2 .li3 a");
    var oDiv3=$("#header .ul2 .li3 .own");
    var oDiva3=$("#header .ul2 .li3 .own a");
    var li4=$("#header .ul2 .li4");
    var li4a=$("#header .ul2 .li4 a");
    var oDiv4=$("#header .ul2 .li4 .own");
    var oDiva4=$("#header .ul2 .li4 .own a");
    var menuli=$("#menu ul li");
    var menulia=$("#menu ul li a");
    var menuli8=$("#menu ul .navhover") ;
    var searchBtn=$("#search-button")
    var login=$("#header .login");
    var regist=$("#header .regist");
    var tuichu=$("#header .tuichu");

    li1.mouseenter(function(){
        li1a.css({borderLeft:"1px solid #ccc",borderRight:"1px solid #ccc"});
        oDiva.css({border:"none"});
        oDiv.stop(true).slideDown(600,function(){
            oDiv.animate({opacity:1},500);
        });
    });
    li1.mouseleave(function(){
        li1a.css({borderLeft:"1px solid #fff",borderRight:"1px solid #fff"});
            oDiv.stop(true).slideUp(50,function(){
        });
    });

    li2.mouseenter(function(){
        li2a.css({borderLeft:"1px solid #ccc",borderRight:"1px solid #ccc"});
        oDiva2.css({border:"none"});
        oDiv2.stop(true).slideDown(600,function(){
            oDiv2.animate({opacity:1},500);
        });
    });
    li2.mouseleave(function(){
        li2a.css({borderLeft:"1px solid #fff",borderRight:"1px solid #fff"});
        oDiv2.stop(true).slideUp(50,function(){
        });

    });
    li3.mouseenter(function(){
        li3a.css({borderLeft:"1px solid #ccc",borderRight:"1px solid #ccc"});
        oDiva3.css({border:"none"});
        oDiv3.stop(true).slideDown(600,function(){
            oDiv3.animate({opacity:1},500);
        });
    });
    li3.mouseleave(function(){
        li3a.css({borderLeft:"1px solid #fff",borderRight:"1px solid #fff"});
        oDiv3.stop(true).slideUp(50,function(){
        });
    });
    li4.mouseenter(function(){
        li4a.css({borderLeft:"1px solid #ccc",borderRight:"1px solid #ccc"});
        oDiva4.css({border:"none"});
        oDiv4.stop(true).slideDown(600,function(){
            oDiv4.animate({opacity:1},500);
        });
    });
    li4.mouseleave(function(){
        li4a.css({borderLeft:"1px solid #fff",borderRight:"1px solid #fff"});
        oDiv4.stop(true).slideUp(50,function(){
        });
    });

    searchBtn.mouseenter(function(){
        searchBtn.css({backgroundPosition:'-310px -39px'});

    });
    searchBtn.mouseleave(function(){
       searchBtn.css({backgroundPosition:'right 0'});

    });
  /*nav部分*/
    menuli.mouseenter(function(){
         iIndex=$(this).index();
        menulia.eq(iIndex).css({color:"#eb744f"});
        if(iIndex==0){
            menuli8.stop(true).animate({left:20},200,function(){
                menuli8.animate({opacity:1})
            })
        }else{
            menuli8.stop(true).animate({left:iIndex*104},200,function(){
                menuli8.animate({opacity:1})
            })
        }

    });
    menuli.mouseleave(function(){
        menulia.eq(iIndex).css({color:"#000"});
        menuli8.stop(true).animate({left:-50},200,function(){})
    });
    menuli8.mouseenter(function(){

        menuli8.stop(true);
    });
 /*main部分*/
    var fa1=$(".fullbox .li2  .dl1");
    var fa1i=$(".fullbox .li2  .a1 i");
    var fa2=$(".fullbox .li2  .dl2");
    var fa2i=$(".fullbox .li2  .a2 i");
    var fa3=$(".fullbox .li2  .dl3");
    var fa3i=$(".fullbox .li2  .a3 i");
    var fa4=$(".fullbox .li2  .dl4");
    var fa4i=$(".fullbox .li2  .a4 i");
    var fa5=$(".fullbox .li2  .dl5");
    var fa5i=$(".fullbox .li2  .a5 i");
    var fa6=$(".fullbox .li2  .dl6");
    var fa6i=$(".fullbox .li2  .a6 i");
    var fa7=$(".fullbox .li2  .dl7");
    var fa7i=$(".fullbox .li2  .a7 i");
    var fa8=$(".fullbox .li2  .dl8");
    var fa8i=$(".fullbox .li2  .a8 i");
    var fa9=$(".fullbox .li2  .dl9");
    var fa9i=$(".fullbox .li2  .a9 i");

    fa1.mouseenter(function(){
        fa1.css({color:"#fff",background:"url(../images/common2.png) 0 -3px no-repeat #e44715"})
        fa1i.css({display:"block"});
        fa1.children("dd").css({display:"block"})
    });
    fa1.mouseleave(function(){
        fa1.css({color:"#666",background:"url(../images/common3.png) 0 -3px no-repeat #fff"})
        fa1i.css({display:"none"});
        fa1.children("dd").css({display:"none"});
    });
    fa2.mouseenter(function(){
        fa2i.css({display:"block"});
        fa2.children("dd").css({display:"block"});
        fa2.css({color:"#fff",background:"url(../images/common2.png) 0 -97px no-repeat #e44715"})
    });
    fa2.mouseleave(function(){
        fa2i.css({display:"none"});
        fa2.children("dd").css({display:"none"});
        fa2.css({color:"#666",background:"url(../images/common3.png) 0 -97px no-repeat #fff"})
    });
    fa3.mouseenter(function(){
        fa3i.css({display:"block"});
        fa3.children("dd").css({display:"block"});
        fa3.css({color:"#fff",background:"url(../images/common2.png) 0 -192px no-repeat #e44715"})
    });
    fa3.mouseleave(function(){
        fa3i.css({display:"none"});
        fa3.children("dd").css({display:"none"});
        fa3.css({color:"#666",background:"url(../images/common3.png) 0 -192px no-repeat #fff"})
    });
    fa4.mouseenter(function(){
        fa4i.css({display:"block"});
        fa4.children("dd").css({display:"block"});
        fa4.css({color:"#fff",background:"url(../images/common2.png) 0 -226px no-repeat #e44715"})
    });
    fa4.mouseleave(function(){
        fa4i.css({display:"none"});
        fa4.children("dd").css({display:"none"});
        fa4.css({color:"#666",background:"url(../images/common3.png) 0 -226px no-repeat #fff"})
    });
    fa5.mouseenter(function(){
        fa5i.css({display:"block"});
        fa5.children("dd").css({display:"block"});
        fa5.css({color:"#fff",background:"url(../images/common2.png) 0 -289px no-repeat #e44715"})
    });
    fa5.mouseleave(function(){
        fa5i.css({display:"none"});
        fa5.children("dd").css({display:"none"});
        fa5.css({color:"#666",background:"url(../images/common3.png) 0 -289px no-repeat #fff"})
    });
    fa6.mouseenter(function(){
        fa6i.css({display:"block"});
        fa6.children("dd").css({display:"block"});
        fa6.css({color:"#fff",background:"url(../images/common2.png) 0 -161px no-repeat #e44715"})
    });
    fa6.mouseleave(function(){
        fa6i.css({display:"none"});
        fa6.children("dd").css({display:"none"});
        fa6.css({color:"#666",background:"url(../images/common3.png) 0 -161px no-repeat #fff"})
    });
    fa7.mouseenter(function(){
        fa7i.css({display:"block"});
        fa7.children("dd").css({display:"block"});
        fa7.css({color:"#fff",background:"url(../images/common2.png) 0 -386px  no-repeat #e44715"})
    });
    fa7.mouseleave(function(){
        fa7i.css({display:"none"});
        fa7.children("dd").css({display:"none"});
        fa7.css({color:"#666",background:"url(../images/common3.png) 0 -386px no-repeat #fff"})
    });
    fa8.mouseenter(function(){
        fa8i.css({display:"block"});
        fa8.children("dd").css({display:"block"});
        fa8.css({color:"#fff",background:"url(../images/common2.png) 0 -321px no-repeat #e44715"})
    });
    fa8.mouseleave(function(){
        fa8i.css({display:"none"});
        fa8.children("dd").css({display:"none"});
        fa8.css({color:"#666",background:"url(../images/common3.png) 0 -321px no-repeat #fff"})
    });
    fa9.mouseenter(function(){
        fa9i.css({display:"block"});
        fa9.children("dd").css({display:"block"})
        fa9.css({color:"#fff",background:"url(../images/common2.png) 0 -129px no-repeat #e44715"})
    });
    fa9.mouseleave(function(){
        fa9i.css({display:"none"});
        fa9.children("dd").css({display:"none"});
        fa9.css({color:"#666",background:"url(../images/common3.png) 0 -129px no-repeat #fff"})
    });

/*banner图*/
    var leftBtn=$(".left-btn");
    var rightBtn=$(".right-btn");
    var banner=$(".banner");
    var aImgListLi=$(".img-list li");
    var aBtnList=$(".btn-list a");
    var dex=0;
    var  oTimer=null;
    banner.mouseenter(function(){
        leftBtn.css({display:"block"}).animate({left:20},500);
        rightBtn.css({display:"block"}).animate({right:20},500)

    });
    banner.mouseleave(function(){
        leftBtn.css({display:"none"}).animate({left:40},300);
        rightBtn.css({display:"none"}).animate({right:40},300);

    });
  // 按钮列表添加click事件

    aBtnList.click(function(){
        dex=$(this).index();
        aImgListLi.animate({
            opacity: 0,
        }, function () {
            $(this).css('display', 'none');
        });
        aImgListLi.eq(dex).css({display:'block'}).stop(true).animate({
            opacity: 1
        });

        // 改变按钮的class名称
        aBtnList.removeClass("active").eq(dex).addClass("active");
    });


// 给左侧按钮添加click事件
    leftBtn.click(function () {
        dex--;
        if(dex < 0) {
            dex = aImgListLi.length - 1;
        }
        aImgListLi.animate({
            opacity: 0,
        }, function () {
            $(this).css('display', 'none');
        });
        aImgListLi.eq(dex).css({display:'block'}).stop(true).animate({
            opacity: 1
        });
        // 改变按钮的class名称
        aBtnList.removeClass('active').eq(dex).addClass('active');
    });

// 给右侧按钮添加click事件
    rightBtn.click(function () {
        dex++;
        if(dex === aImgListLi.length) {
            dex = 0;
        }
        aImgListLi.animate({
            opacity: 0,
        }, function () {
            $(this).css('display', 'none');
        });
        aImgListLi.eq(dex).css({display:'block'}).stop(true).animate({
            opacity: 1
        });

        // 改变按钮的class名称
        aBtnList.removeClass('active').eq(dex).addClass('active');
    });

    // 自动轮播
    oTimer = setInterval(function () {
        dex++;
        if(dex === aImgListLi.length) {
            dex = 0;
        }
        aImgListLi.animate({
            opacity: 0,
        }, function () {
            $(this).css('display', 'none');
        });
        aImgListLi.eq(dex).css({display:'block'}).stop(true).animate({
            opacity: 1
        });

        // 改变按钮的class名称
        aBtnList.removeClass('active').eq(dex).addClass('active');
    }, 2000);

    banner.mouseenter(function () {
        clearInterval(oTimer);
    });
    banner.mouseleave(function () {
        oTimer = setInterval(function () {
            dex++;
            if(dex === aImgListLi.length) {
                dex = 0;
            }
            aImgListLi.animate({
                opacity: 0,
            }, function () {
                $(this).css('display', 'none');
            });
            aImgListLi.eq(dex).css({display:'block'}).stop(true).animate({
                opacity: 1
            });

            // 改变按钮的class名称
            aBtnList.removeClass('active').eq(dex).addClass('active');
        }, 2000);
    });


//吸顶

    var ebox=$(".ebox");
    var sp_back=$(".sp-back");
    var li1_back=$("#back .li1");
    var li1_box=$("#back .li1 .li1box");
    var li1_del=$("#back .li1 .li1box");
    var li2_back=$("#back .li2");
    var li2_box=$("#back .li2 div");
    var li2_del=$("#back .li2 div span");
    var li3_back=$("#back .li3 ");
    var li3_box=$("#back .li3 div ");
    var li3_del=$("#back .li3 div .ui-dialog-close");
    var li4_back=$("#back .li4");
    var li5_back=$("#back .li5");

    li1_back.click(function(){
        li1_box.css({display:"block"})
    });
    li1_del.click(function(){
        //return false;写在前面没效果
        li1_box.css({display:"none"});
        return false;
    });

    li2_back.click(function(){
        li2_box.css({display:"block"})
    });
    li2_del.click(function(){
        li2_box.css({display:"none"});
        return false;

    });
    li3_back.click(function(){
        $("#screen_locker").css({display:"block"});
        li3_box.css({display:"block"})
    });
    li3_del.click(function(){
        $("#screen_locker").css({display:"none"});
        li3_box.css({display:"none"});
        return false;

    });








    li4_back.mouseenter(function(){
        ebox.css({display:"block"});
    });
    li4_back.mouseleave(function(){
        ebox.css({display:"none"});
    });
    li5_back.mouseenter(function(){
        li5_back.css({background:"none"});
        sp_back.css({opacity:"1",background:"#ccc"});
    });
    li5_back.mouseleave(function(){
        li5_back.css({background:"url(../images/commonb.png) -1px 2px no-repeat #eee"});
        sp_back.css({opacity:"0"});
    });

    //点击回到顶部按钮，页面滚动距离为零
    li5_back.click(function(){
        /*document.body.scrollTop = 0;*/
       $("body,html").animate({scrollTop:0},2000);
    });

    $(window).scroll(function(){
        /*var scrolltop=$(document).scrollTop();*/
        var scrolltop=document.body.scrollTop||document.documentElement.scrollTop;
        /*判断回到顶部按钮出现和消失*/
        if(scrolltop<=1660){
            li5_back.css({display:"none"});
            $(".fixedbox").css({
                position:"absolute",
                top:'1586px'
            })
        }
        else{
            li5_back.css({display:"block"});
            $(".fixedbox").css({
                position:"fixed",
                top:'0'
            })
        }
      //判断滚动距离，让对应的li背景为选中颜色
        if(scrolltop<2834){
            fixedboxLi1.addClass("current");
        }else {
            fixedboxLi1.removeClass("current");
        }
        if(scrolltop>=2834&&scrolltop<4078){
            fixedboxLi2.addClass("current");
        }else{
            fixedboxLi2.removeClass("current");
        }
        if(scrolltop>=4078&&scrolltop<5332){
            fixedboxLi3.addClass("current");
        }else{
            fixedboxLi3.removeClass("current");
        }
        if(scrolltop>=5332&&scrolltop<6153){
            fixedboxLi4.addClass("current");
        }else{
            fixedboxLi4.removeClass("current");
        }
        if(scrolltop>=6153&&scrolltop<7000){
            fixedboxLi5.addClass("current");
        }else{
            fixedboxLi5.removeClass("current");
        }
    });

    /*点击每个li后页面滚动到相应商品部分*/
        var fixedboxLi1 =$(".tofloor1");
        var fixedboxLi2 =$(".tofloor2");
        var fixedboxLi3 =$(".tofloor3");
        var fixedboxLi4 =$(".tofloor4");
        var fixedboxLi5 =$(".tofloor5");
    fixedboxLi1.click(function(){
        $("body,html").animate({scrollTop:1590},1000);
    });
    fixedboxLi2.click(function(){
        $("body,html").animate({scrollTop:2834},1000);
    });
    fixedboxLi3.click(function(){
        $("body,html").animate({scrollTop:4078},1000);
    });
    fixedboxLi4.click(function(){
        $("body,html").animate({scrollTop:5332},1000);
    });
    fixedboxLi5.click(function(){
        $("body,html").animate({scrollTop:6153},1000);
    });

//登录
    $(".login").click(function(){
        $("#screen_locker").css({display:"block"});
     $("#loginpage").css({display:"block"}).animate({right:0},1000)
    });
    $("#loginpage .delx").click(function(){
        $("#screen_locker").css({display:"none"});
        $("#loginpage").animate({right:-1150},1000)
    });
//注册
     $(".regist").click(function(){
        $("#screen_locker").css({display:"block"});
        $("#registpage").css({display:"block"}).animate({right:0},1000)
    });
    $("#registpage .delx").click(function(){
        $("#screen_locker").css({display:"none"});
        $("#registpage").animate({right:-1150},1000)
    });
//点击登录两个字，改变内容
    var btn=true;
    $(".tologin").click(function(){
        if(btn){
            $(".user2").text("用户登录");
            $(".username2").attr("placeholder","邮箱/用户名");
            $(".i2").text("下次自动登录");
            $(".i2").css({background:'url(../images/weibo.png) no-repeat'});
            $(".i2").css({backgroundPosition:'0 -354px',paddingLeft:'20px'});
            $(".input2").val("立即登录");
            $(".tologin").text("注册");
            btn=false;
        }else{
            $(".user2").text("用户注册");
            $(".username2").attr("placeholder","邮箱");
            $(".i2").text("《卡当注册协议》");
            $(".i2").css({background:'none',paddingLeft:'0px'});
            $(".input2").val("同意协议并注册");
            $(".tologin").text("登录");
            btn=true;
        }

    });
    //点击注册两个字，改变内容
     var btn1=true;
    $(".toregister").click(function(){
        if(btn1){
        $(".user1").text("用户注册");
            $(".username1").attr("placeholder","邮箱");
            $(".i1").text("《卡当注册协议》");
            $(".i1").css({background:'none',paddingLeft:'0px'});
            $(".input1").val("同意协议并注册");
            $(".toregister").text("登录");
            btn1=false;
        }
        else{
            $(".user1").text("用户登录");
            $(".username1").attr("placeholder","邮箱/用户名");
            $(".i1").text("下次自动登录");
            $(".i1").css({background:'url(../images/weibo.png) no-repeat'});
            $(".i1").css({backgroundPosition:'0 -354px',paddingLeft:'20px'});
            $(".input1").val("立即登录");
            $(".toregister").text("注册");
            btn1=true;
        }
    });


//复选框选择
    var btn3=true;
    $(".i1").click(function(){
        if(btn3){
            $(".i1").css({backgroundPosition:'0 -390px'});
            btn3=false;
        }else{
            $(".i1").css({backgroundPosition:'0 -354px'});
            btn3=true;
        }

    });

    var btn4=true;
    $(".i2").click(function(){
        if(btn4){
            $(".i2").css({backgroundPosition:'0 -390px'});
            btn4=false;
        }else{
            $(".i2").css({backgroundPosition:'0 -354px'});
            btn4=true;
        }

    });

    /*正则判断登录注册*/

    //注册
    var username2=$(".username2");
    var password2=$(".password2");
    var reg1=new RegExp(/^\w{1,}@\w{1,}\.\w{1,}$/);
    var reg2=new RegExp(/^\w{6,20}$/);
    var reg3=/^\s*$/;


    username2.blur(function(){
         val1=username2.val();
       if(reg3.test(val1)==true){
            $(".text2_p").text("邮件不能为空")
        }else {
           if (reg1.test(val1) == false) {
               $(".text2_p").text("邮件格式不正确")
           } else {
               $(".text2_p").text("");
           }
       }
    });
    username2.bind("input propertychange",function(){
        val1=username2.val();
        if(reg3.test(val1)){
            $(".text2_p").text("邮件不能为空")
        }else if(reg1.test(val1)==false){
            $(".text2_p").text("邮件格式不正确")
        }else if(reg1.test(val1)==true){
            $(".text2_p").text("")
        }
    });
    password2.blur(function(){
         val2=password2.val();
        if(reg3.test(val2)==true){
            $(".psw2_p").text("密码不能为空")
        }else {
            if(reg2.test(val2) == false){
                $(".psw2_p").text("密码长度介于6-20之间")
            }else{
                $(".psw2_p").text("");
                //btn5=true;
            }
        }
    });
    password2.bind("input propertychange",function(){
        var val2=password2.val();
        if(reg3.test(val2)==true){
            $(".psw2_p").text("密码不能为空")
        }else {
            if (reg2.test(val2) == false) {
                $(".psw2_p").text("密码长度介于6-20之间")
            } else {
                $(".psw2_p").text("")
            }
        }
    });

    $(".input2").click(function(){

        if((reg1.test(val1) == true)&&(reg2.test(val2) == true)){
            $("#screen_locker").css({display:"none"});
            $("#registpage").animate({right:-1150},1000);
            setCookie('name',val1, 7, '/');
            setCookie('pass',val2, 7, '/');
        }
    });
    $(".hi").text(getCookie('name'));
    //console.log($(".hi").text());

    /*var cook= getCookie('name');
     if(cook){
     login.html(" ");
     regist.html(" ");
     tuichu.html("退出");
     $(".hi").html(getCookie('name'));
     }else{
     login.html("登录");
     regist.html("注册");
     tuichu.html(" ");
     $(".hi").html("欢迎来到卡当网");
     }

     tuichu.click(function(){
     login.html("登录");
     regist.html("注册");
     tuichu.html(" ");
     $(".hi").html("欢迎来到卡当网");
     removeCookie('name','/')
     });*/

 //登录
    var username1=$(".username1");
    var password1=$(".password1");
    username1.val(getCookie("name"));
    password1.val(getCookie("pass"));

    //var reg4=new RegExp(/^\w{1,}@\w{1,}\.\w{1,}$/);
    var reg5=new RegExp(/^\w{6,20}$/);


    $(".input1").click(function(){
            val3=username1.val();
            val4=password1.val();
            if(reg3.test(val3)==true){
                $(".text1_p").text("邮件不能为空");
                return;
            }else{
                if(reg3.test(val4)==true){
                    $(".psw1_p").text("密码不能为空");
                    return;
                }else{
                      if (reg5.test(val4) == false) {
                                $(".psw1_p").text("密码长度介于6-20之间");
                      } else {

                           if((val3==getCookie("name"))&&(val4==getCookie("pass"))){
                               //判断登陆的值是否等于cookie里面记录的注册的账号密码
                               //$(".psw1_p").text("");
                                alert(111111);
                                 $(".hi").text(val3);
                                 $("#loginpage").animate({right:-1150},1000);
                                 $("#screen_locker").css({display:"none"});


                           } else{
                                alert("账户不存在")
                                //$(".psw1_p").text("账户不存在");
                           }
                            /*//if cookie.比较是否有此账户
                                if(){

                                }else{
                                    if(){

                                    }else{
                                     //改变hi
                                    }
                                }
                            // 有的话判断密码是否正确
                                //没有就p为账户不存在*/



                  }
                    password1.bind("input propertychange",function(){
                        var val4=password1.val();
                        if (reg5.test(val4) == false) {
                            $(".psw1_p").text("密码长度介于6-20之间");
                        }
                        else{
                            $(".psw1_p").text("");
                        }
                    })
                }
            }

    })













});